﻿using Common;
using DAL.AntiFake;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.AntiFake
{
    public class DigitCodeBLL
    {

      private QueryCodeLogDAL logDal = new QueryCodeLogDAL();
        private DigitcodeDAL dal=new DigitcodeDAL();


          /// <summary>
         /// 物流码 防伪码  互查
         /// </summary>
         /// <returns></returns>
        public string DigitCodeData(string code,string type)
        {
            if (type=="fw")
            {
           
                string strCode = code;
                string tbl = strCode.Substring(0, 6);
                return dal.GetDigitFw_CodeData(tbl, strCode);
            
            }
            else
            {
                string key = AppConst.DigitCodeKey;
                string strCode = MiMa.Jiemi(code, key);
                string tbl = strCode.Substring(0, 6);
                return dal.GetDigitWl_Code(tbl, strCode);
            }
        }

       
        #region 分页数据
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pager"></param>
        /// <returns></returns>
        public DataTable GetPagerList(ref PagerModel pager, string type, string code)
        {
            DataTable dt = new DataTable();
            if (!string.IsNullOrEmpty(code))
            {
            if (type == "wl_Code")//根据防伪  查询物流
            {
                string key = AppConst.DigitCodeKey;
                 code = MiMa.Jiemi(code, key);
                 if (!string.IsNullOrEmpty(key))
                 {
                     pager.StrCondition = " Fw_Code ='" + code + "'";
                 }
                 code = code.Substring(0, 6);
                 if (!dal.TabExists(code))
                 {
                     return dt;
                 }
            }
            else
            {
                pager.StrCondition = " Wl_Code ='" + code + "'";
                code = code.Substring(0, 6);
            }

            }
          dt=dal.GetPagerList(ref pager, code);
          return dt;
        }
        #endregion

        #region 查询是否有对应防伪码
        public DataTable GetModel(string tablePart, string Fw_Code, string Wl_Code,string ipaddress,string privince,string city)
        {
            return dal.GetModel(tablePart, Fw_Code, Wl_Code,ipaddress,privince,city);
        }
        /// <summary>
        /// 根据溯源码查询实体类
        /// </summary>
        /// <returns></returns>
        public DataTable GetModelByWlCode(string tabName, string wl_code)
        {
            return dal.GetModelByWlCode(tabName, wl_code);
        }
        public bool UpdateCount(string tablePart, string count, string id, string flag)
        {
            return dal.UpdateCount(tablePart,count, id, flag);
        }
        #endregion

        #region 表是否存在
        public bool TabExists(string code)
        {
            return dal.TabExists(code);
        }
        #endregion
        /// <summary>
        /// 获取查询记录
        /// </summary>
        /// <returns></returns>
        public DataTable getQueryLog(string code) {
            return dal.getQueryLog(code);
        }
    }
}
